package com.wellwan.cloud.fallbackfactoty;

import com.wellwan.cloud.entity.User;
import com.wellwan.cloud.feignclient.UserFeignClient;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Copyright (C),微动天下
 * FileName: FeignClientFallbackFactory
 * Author:   maokai
 * Date:     2020/2/29 11:24
 * Description:
 */
@Component
@Slf4j
public class FeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {
    @Override
    public UserFeignClient create(Throwable throwable) {

//        Type type = getClass().getGenericSuperclass();
//        Type[] parameter = ((ParameterizedType) type).getActualTypeArguments();
//        String typeName = parameter[0].getTypeName();
//        log.info("typeName------>{}",typeName);
        return new UserFeignClient() {
            @Override
            public User findById(Long id) {
                log.error("------>{}",throwable.getMessage());
                log.error("异常信息------->{}","默认用户");
                User user = new User();
                user.setId(-1L);
                user.setName("默认用户");
                return user;
            }

            @Override
            public String handleFileUpload(MultipartFile file) {
                String errorMessage = "上传文件失败";
                log.error("异常信息------->{}",errorMessage);
                return errorMessage;
            }
        };
    }
}
